package cn.dw.mm.activiti;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.impl.persistence.entity.UserEntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.dw.mm.domain.Role;
import cn.dw.mm.domain.UserRole;
import cn.dw.mm.mapper.RoleMapper;
import cn.dw.mm.mapper.UserMapper;
import cn.dw.mm.mapper.UserRoleMapper;

/**
 * 自定义用户管理类，管理用户方法
 * 添加其他方法
 */
@Component
public class CustomUserEntityManager extends UserEntityManager{
    @Autowired
    private UserMapper cusUserMapper;
    @Autowired
    private RoleMapper cusRoleMapper;
    @Autowired
    private UserRoleMapper cusUserRoleMapper;
    @Override
    public User findUserById(String userId){
        User userEntity=new UserEntity();
        cn.dw.mm.domain.User rocIdUser = cusUserMapper.selectByPrimaryKey(userId);
        //将自定义的user转化为activiti的类
        userEntity=ActivitiUserUtils.toActivitiUser(rocIdUser);
        //返回activiti的实体类
        return userEntity;
    }

    @Override
    public List<Group> findGroupsByUser(final String userId) {
        if(userId==null){
            return null;
        }
        List<Role> roleList=new ArrayList<Role>();
        List<UserRole> userRoleList = cusUserRoleMapper.selectByUserId(userId);
        for (UserRole userrole:userRoleList
             ) {
            String roleId = userrole.getRoleId();
            Role role = cusRoleMapper.selectByPrimaryKey(roleId);
            roleList.add(role);
        }
        List<Group> gs=null;
        gs=ActivitiUserUtils.toActivitiGroups(roleList);
        return gs;
    }
}
